package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import static com.shanxia.codec.Decoder.bytesToIntLittle;
import static com.shanxia.codec.Decoder.intToBytesLittle;

/**
 * S -> C
 * 服务器下发升级文件数据结束指令
 */
@Data
public class Cmd1009 extends ProtocolDataBase {

    public static final int MIN_SIZE = 4;

    private int v1;

    public Cmd1009(){
        super.setCmd(1009);
    }

    public static Cmd1009 decodeFromBase(ProtocolDataBase protocolDataBase) {

        Cmd1009 cmd = new Cmd1009();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        cmd.v1 = bytesToIntLittle(dataField, 0);
        return cmd;
    }

    @Override
    public byte[] getDataField() {
        byte[] data = new byte[MIN_SIZE];
        intToBytesLittle(data, 0, v1);
        return data;
    }
}
